<?php
  include ('header.php');
  require_once('includes/db_connect.inc.php');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><?php echo $titles['edit_all_inventory'] ?></title>	

</head>

<body>

<?php 
	//Сортирвока по инв. номеру
	if (isset($_GET[sort_in])){
		$_SESSION[sort_in]=$_GET[sort_in];
	}
	
	if (isset($_GET[sort_name])){
		$_SESSION[sort_name]=$_GET[sort_name];
	}
	if (isset($_GET[sort_type])){
		$_SESSION[sort_type]=$_GET[sort_type];
	}

	if (isset($_POST['inv_numb_filter'])){
	$_SESSION['inv_numb_filter']=$_POST['inv_numb_filter'];
	} 
	
	if (isset($_POST['name_filter'])){
	$_SESSION['name_filter']=$_POST['name_filter'];
	} 
	
	if (isset($_POST['item_type'])){
	$_SESSION['item_type']=$_POST['item_type'];
	} 
	
	if (isset($_POST['auditory'])){
	$_SESSION['auditory']=$_POST['auditory'];
	} 
	
	if (isset($_POST['status'])){
	$_SESSION['status']=$_POST['status'];
	} 
	
function create_image ($sort_type){
switch ($_SESSION["lang"]) {
case "ru":   include_once ('lang/ru.lang'); break;
case "ua":   include_once ('lang/ua.lang'); break;
default: include_once ('lang/ua.lang'); break;
};
if($sort_type==$_SESSION['sort_type'])
{

switch ($_SESSION[sort_name]) {
case 'no':echo"<a href='".$_SERVER['PHP_SELF']."?sort_name=incr&sort_type=".$sort_type."'><img src='images/sort_disable.png' border=0 onMouseOver=\"toolTip('".$actions['sort']."')\" onMouseOut=\"toolTip()\"></a>";break;
case 'incr':echo"<a href='".$_SERVER['PHP_SELF']."?sort_name=decr&sort_type=".$sort_type."'><img src='images/sort_incr.png' border=0 onMouseOver=\"toolTip('".$actions['sort']."')\" onMouseOut=\"toolTip()\"></a>";break;
case 'decr':echo"<a href='".$_SERVER['PHP_SELF']."?sort_name=no&sort_type=".$sort_type."'><img src='images/sort_decr.png' border=0 onMouseOver=\"toolTip('".$actions['sort']."')\" onMouseOut=\"toolTip()\"></a>";break;
default:echo"<a href='".$_SERVER['PHP_SELF']."?sort_name=incr&sort_type=".$sort_type."'><img src='images/sort_disable.png' border=0 onMouseOver=\"toolTip('".$actions['sort']."')\" onMouseOut=\"toolTip()\"></a>";break;
}
echo "<div id=\"floatTip\"></div>";
}else {

echo"<a href='".$_SERVER['PHP_SELF']."?sort_name=incr&sort_type=".$sort_type."'><img src='images/sort_disable.png' border=0 onMouseOver=\"toolTip('".$actions['sort']."')\" onMouseOut=\"toolTip()\"></a>";
	echo "<div id=\"floatTip\"></div>";}


}
	?>

<form name='all_inventory' method="POST">
<?php /*
foreach ($_POST as $key=>$val) {
echo $key ."|". $val . "<br />";}
*/
?>


   <TABLE  class="ALL_INVENTORY_UP">
    <TR>
	 </td>
	 
	<td align=left width=100>
	 <?php
   if($_SESSION['access']>1){
   ?>
	<a href='item_all_inventory.php'> <img src='images/add.png' border=0  width=25 height=25 alt='Додати' onMouseOver="toolTip('<?php echo $actions['add_new']?>')" onMouseOut="toolTip()"></a>
	<a href='javascript: void(0);' onClick="if (confirm('<?php echo $actions['confirm_delete']?>')) {document.forms['all_inventory'].submit() }"><img src='images/delete.png'	border=0  width=25 height=25 onMouseOver="toolTip('<?php echo $actions['delete_checked']?>')" onMouseOut="toolTip()"></img></a>
	 <?php
   }
   ?>
   
	<a href="excel_export.php"><img src='images/excel.png' border=0  width=25 height=25 alt='Экспорт в excel' onMouseOver="toolTip('<?php echo $actions['export_excel']?>')" onMouseOut="toolTip()"></a>	
	</td>
	</tr><tr>
	<td align="LEFT" width='*'>
	Фільтри:
	по назві <input type="text" name="name_filter" size=20 onchange='document.forms["all_inventory"].submit();' value="<?php echo $_SESSION['name_filter']?>">
	
	по інв. номеру: <input type="text" name="inv_numb_filter" size=8  onchange='document.forms["all_inventory"].submit();' value="<?php echo $_SESSION['inv_numb_filter']?>">
	по типу: 
	<SELECT NAME="item_type" onchange='document.forms["all_inventory"].submit();'>
<?php 




$sql2="SELECT * FROM inventory_type order by type_id";  
$process2=mysql_query($sql2, $db);
$i=1;
while ($row2 = mysql_fetch_array($process2, $i)){
$selected=($row2[type_id]==$_SESSION['item_type']) ? ' selected' :'';
echo "<option value='".$row2[type_id]."' ".$selected.">".$row2[type_name]."</option>";
}
?>
</SELECT>
по аудиторії: 
	<SELECT NAME="auditory" onchange='document.forms["all_inventory"].submit();'>
<?php 


$sql2="SELECT * FROM auditories order by auditory_id";  
$process2=mysql_query($sql2, $db);
$i=1;
while ($row2 = mysql_fetch_array($process2, $i)){
$selected=($row2[auditory_id]==$_SESSION['auditory']) ? ' selected' :'';
echo "<option value='".$row2[auditory_id]."' ".$selected.">".$row2[auditory_name]."</option>";
}
?>
</SELECT>

по статусу: 
	<SELECT NAME="status" onchange='document.forms["all_inventory"].submit();'>
<?php 


$sql2="SELECT * FROM item_status order by status_id";  
$process2=mysql_query($sql2, $db);
$i=1;
while ($row2 = mysql_fetch_array($process2, $i)){
$selected=($row2[status_id]==$_SESSION['status']) ? ' selected' :'';
echo "<option value='".$row2[status_id]."' ".$selected.">".$row2[status_name]."</option>";
}
?>
</SELECT>

  	</tr>
    </TABLE>

<?php




?>
	
<TABLE  class="ALL_INVENTORY" width='95%'>
<TR >
   <?php
   if($_SESSION['access']>1){
   ?>
   <td class="ALL_INVENTORY" width='10'><?php echo $texts['choise']?></td>
    <?php
   }
   ?>
	<td class="ALL_INVENTORY_NBR" width='*'><?php echo $texts['name']?> </td>
	<td width='5' class="ALL_INVENTORY_NBL"> <?php create_image('item_name') ?> </td>
	<td class="ALL_INVENTORY_NBR" width='70'> <?php echo $texts['date_open']?></td>
	<td width='5' class="ALL_INVENTORY_NBL"> <?php create_image('date_open') ?> </td>
	<td class="ALL_INVENTORY_NBR" width='20'><?php echo $texts['inv_numb']?> </td>
	<td width='5' class="ALL_INVENTORY_NBL"> <?php create_image('inv_numb') ?> </td>
	<td class="ALL_INVENTORY_NBR" width='30'><?php echo $texts['count']?></td>
	<td width='5' class="ALL_INVENTORY_NBL"> <?php create_image('count') ?> </td>
	<td class="ALL_INVENTORY_NBR" width='60'><?php echo $texts['one_cost']?> </td>
	<td width='5' class="ALL_INVENTORY_NBL"> <?php create_image('one_cost') ?> </td>
	
	<td class="ALL_INVENTORY" width='90'> <?php echo $texts['it_user']?></td>
	<td class="ALL_INVENTORY" width='120'> <?php echo $texts['status']?></td>
	<td class="ALL_INVENTORY" width='50'><?php echo $texts['auditory']?></td>
	<td class="ALL_INVENTORY" width='30'><?php echo $texts['type']?></td>
	<td class="ALL_INVENTORY" width='100'> <?php echo $texts['function']?></td>
	
</TR>


	
<?php 

$_SESSION['to_excel']="<TABLE  border=1 width='95%'>
<TR >
	<td  width='200' align=left> Назва по бухг</td>
	<td  width='200' align=left> Назва як є</td>
	<td  width='60'> Ном. номер</td>
	<td  width='60'>  Внутрішній номер</td>
	<td  width='60'>  Рах/ субрах </td>
	<td  width='90'> Дата відкриття</td>
	<td  width='60'>  Одиниця виміру</td>
	<td  width='60'> Наймену вання виміру </td>
	<td  width='90' >  Інвентарний номер </td>
	<td  width='40'>  Кількість</td>
	<td  width='40'>  Код</td>
	<td  width='80'> Ціна одного</td>
	<td  width='120'> Відповідальний</td>
	<td  width='120'> Статус</td>
	<td  width='80'> Аудиторія</td>
	<td  width='60'> Тип</td>
	
</TR>
";

if ($_GET[del]){
$sql="DELETE FROM all_inventory where inv_numb='$_GET[del]'";  
$process=mysql_query($sql, $db);

$sql="DELETE FROM inventory_individual where inv_number='$_GET[del]'";  
$process=mysql_query($sql, $db);
}
//обнуляем
$_GET[del]='';
unset ($_GET[del]);

//Если страница не указана по умолчанию 1 страница
if(!$_GET['page']){
$_GET['page']=1;
}
  
//Запрос для выборки инвентаря
$sql="SELECT * FROM all_inventory where inv_numb>''";  
//Запрос для подсчета строк
$sql5 ="select count(*) from all_inventory where inv_numb>''";
//Добавляем условия
//по типу
if ($_SESSION['item_type']){
$sql.=" and item_type=".$_SESSION['item_type'];
$sql5.=" and item_type=".$_SESSION['item_type'];
}

if ($_SESSION['inv_numb_filter']){
$sql.=" and inv_numb like '".$_SESSION['inv_numb_filter']."%'";
$sql5.=" and inv_numb like '".$_SESSION['inv_numb_filter']."%'";
}
if ($_SESSION['auditory']){
$sql.=" and auditory=".$_SESSION['auditory'];
$sql5.=" and auditory=".$_SESSION['auditory'];
}
if ($_SESSION['status']){
$sql.=" and status=".$_SESSION['status'];
$sql5.=" and status=".$_SESSION['status'];
}

if ($_SESSION['name_filter']){
$sql.=" and item_name like '%".$_SESSION['name_filter']."%'";
$sql5.=" and item_name  like '%".$_SESSION['name_filter']."%'";
}

if (isset($_SESSION[sort_name]) && isset($_SESSION[sort_type])){
switch ($_SESSION[sort_name]) {
case 'no':;break;
case 'incr':$sql.=" order by ".$_SESSION[sort_type]." ";break;
case 'decr':$sql.=" order by ".$_SESSION[sort_type]." DESC";break;
}}
//Добавлеяем выборку по инвентарному номеру

//echo $sql;
////////////Разбивка по страницам//////////
//Выбираем количество записей

$process5=mysql_query($sql5, $db);
$count = mysql_fetch_array($process5);
//Получаем целочисленное значение страниц
$count_page=ceil($count[0]/$_SESSION['count_for_page']);
//берем старторвый номер для sql запроса страниц
$start_number=($_GET['page']-1)*$_SESSION['count_for_page'];
//echo $_GET[page];
if (($count_page>1)  && ($_GET[page]!='all')){
$sql.=" LIMIT ".$start_number.",  ".$_SESSION['count_for_page']." ";
}


$n=1;
$process=mysql_query($sql, $db);
while ($row = mysql_fetch_array($process,$n)){ 

$check='check'.$row[inv_numb];
if ($_POST[$check])
{
$sql_del="delete FROM all_inventory where inv_numb='".$row[inv_numb]."'";
$process_del=mysql_query($sql_del, $db);
//echo $sql_del."<br>";
}
}

$process=mysql_query($sql, $db);
$i=1;
$j=0;
while ($row = mysql_fetch_array($process, $i)){ 
$j++;
//echo "<form  action='item_all_inventory.php?inv_number=".$row[inv_numb]."' name='myform".$row[inv_numb]."' Method='POST'>";

echo "<TR class=\"ALL_INVENTORY\">";
    if($_SESSION['access']>1){
		echo "<td class=\"ALL_INVENTORY_L\" ><input type='checkbox' name=check".$row[inv_numb]."></td>";
	}
	$_SESSION['to_excel'].="<tr>";
	echo "<td class=\"ALL_INVENTORY_L\" colspan=2>".$row[item_name]."</td>";
	//Для екселя
	$_SESSION['to_excel'].="<td  >".$row[item_name]."</td>";
	$_SESSION['to_excel'].="<td  >".$row[another_name]."</td>";
    $_SESSION['to_excel'].="<td  >".$row[nom_number]."</td>";
    $_SESSION['to_excel'].="<td  >".$row[sn_internal]."</td>";
    $_SESSION['to_excel'].="<td  >".$row[sub_rah]."</td>";
    $_SESSION['to_excel'].="<td  >".$row[date_open]."</td>";
    $_SESSION['to_excel'].="<td  >".$row[ed_code]."</td>";
    $_SESSION['to_excel'].="<td  >".$row[ed_type]."</td>";
    $_SESSION['to_excel'].="<td  >".$row[inv_numb]."</td>";
	$_SESSION['to_excel'].="<td  >".$row[count]."</td>";
	$_SESSION['to_excel'].="<td  >".$row[code]."</td>";
	$_SESSION['to_excel'].="<td  >". number_format($row[one_cost], 2, ',', '')."</td>";
	
	echo "<td class=\"ALL_INVENTORY\" width=50 colspan=2>".$row[date_open]."</td>";
//	echo "<td class=\"ALL_INVENTORY\" >".$row[ed_code]."</td>";
//	echo "<td class=\"ALL_INVENTORY\" >".$row[ed_type]."</td>";
	echo "<td class=\"ALL_INVENTORY\" colspan=2>".$row[inv_numb]."</td>";
	echo "<td class=\"ALL_INVENTORY\" colspan=2>".$row[count]."</td>";
	echo "<td class=\"ALL_INVENTORY_R\" colspan=2>". number_format($row[one_cost], 2, ',', '')."</td>";
	
	$sql2="SELECT real_name FROM users where id=".$row[manage];  
    $process2=mysql_query($sql2, $db);
	$row2 = mysql_fetch_array($process2);
	
	echo "<td class=\"ALL_INVENTORY\" >".$row2[real_name]."</td>";
	//Для екселя
	$_SESSION['to_excel'].="<td  >".$row2[real_name]."</td>";
	
	$sql2="SELECT * FROM item_status where status_id=".$row[status];  
    $process2=mysql_query($sql2, $db);
	$row2 = mysql_fetch_array($process2);
	
	echo "<td class=\"ALL_INVENTORY\" >".$row2[status_name]."</td>";
	//Для екселя
	$_SESSION['to_excel'].="<td  >".$row2[status_name]."</td>";
	
	$sql2="SELECT * FROM auditories where auditory_id=".$row[auditory];  
    $process2=mysql_query($sql2, $db);
	$row2 = mysql_fetch_array($process2);
	echo "<td class=\"ALL_INVENTORY\" >".$row2[auditory_name]."</td>";
	//Для екселя
	$_SESSION['to_excel'].="<td  >".$row2[auditory_name]."</td>";
	
	
	
	$sql2="SELECT * FROM inventory_type where type_id=".$row[item_type];  
    $process2=mysql_query($sql2, $db);
	$row2 = mysql_fetch_array($process2);
	echo "<td class=\"ALL_INVENTORY\" >".$row2[type_name]."</td>";
	//Для екселя
	$_SESSION['to_excel'].="<td  >".$row2[type_name]."</td>";
	
	$_SESSION['to_excel'].="</tr>";
	
	
	
	
	echo "<td class=\"ALL_INVENTORY\" width='90'>";
	if($_SESSION['access']>1){
	echo "<a href='item_all_inventory.php?inv_number=".$row[inv_numb]."'><img src=images/edit0.png border=0  width=17 height=17 onMouseOver=\"toolTip('".$actions['edit']."')\" onMouseOut=\"toolTip()\"></a> ";
	}
	if($row[item_type]==1){
	echo "<a href='inv_card.php?inv_number=".$row[inv_numb]."'><img src=images/new_card.png border=0 width=17 height=17 onMouseOver=\"toolTip('".$actions['new_card']."')\" onMouseOut=\"toolTip()\"></a> ";
	
	}else {
	echo " <img src=images/no_new_card.png border=0  width=17 height=17 onMouseOver=\"toolTip('".$actions['no_new_card']."')\" onMouseOut=\"toolTip()\">";
	}
	echo "<a href='view_stat_auditory.php?inv_number=".$row[inv_numb]."'>
	<img src=images/view_aud.png border=0  width=17 height=17 onMouseOver=\"toolTip('".$actions['view_auditory']."')\" onMouseOut=\"toolTip()\"></a> ";
	echo "<a href='view_stat_status.php?inv_number=".$row[inv_numb]."'><img src=images/view_status.png border=0  width=17 height=17 onMouseOver=\"toolTip('".$actions['view_status']."')\" onMouseOut=\"toolTip()\"></a> ";
	
	echo "</td>";
	
	echo "<div id=\"floatTip\"></div>";
echo "</TR >";
//echo "</form>";
}
 ?>
 </table>
<?php
if ($count_page>1){
	echo "<center>".$texts[pages]." ";
	if ($_GET[page]!='all'){
	echo "<a href=".$_SERVER['PHP_SELF']."?page=all>".$texts[all_pages]."</a> ";
	}
	else echo $texts[all_pages]." ";
	for ($i=1;$i<=$count_page;$i++){
	if ($_GET[page]!=$i){
		echo "<a href=".$_SERVER['PHP_SELF']."?page=".$i.">$i</a> ";
		}
	else echo "$i ";
	}
	echo "</center>";
}

?>
</form>

</table>
<?php
  include ('footer.php');
?>
</body>
</html>